package Login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-10-21
 * Time: 19:47
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        //获取到用户名和密码
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        //判断用户名和密码是否完整
        if(userName==null || userName.equals("") || password==null || password.equals("")){
            resp.getWriter().write("用户名和密码不完整...");
            return;
        }
        //判断用户名和密码是否正确
        if(!userName.equals("shifangxu") || !password.equals("123456")){
            resp.getWriter().write("用户名或密码不正确!!!");
            return;
        }
        //如果用户名和密码都正确,我们就要创建一个会话
        //这里就代表如果该session(会话)不存在,就会重新创建一个新的会话
        //同时也会自动生成一个sessionId作为key httpSession对象作为value 放进哈希表中
        HttpSession httpSession = req.getSession(true);
        httpSession.setAttribute("userName","shifangxu");
        Integer visitCount = (Integer) httpSession.getAttribute("visitCount");
        if(visitCount==null){
            httpSession.setAttribute("visitCount",0);
        }else {
            //不做处理
        }
        //然后跳转到主页
        resp.sendRedirect("index");
    }
}
